package dk.itu.mmad.returnfromactivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
	
	public static final int SUB_ACTIVITY = 1;
	public static final String SUB_RESULT = "result";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		Button toSubButton = (Button) findViewById(R.id.tosub);
		toSubButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(MainActivity.this, SubActivity.class);
				startActivityForResult(intent, SUB_ACTIVITY);
			}
		});
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(requestCode == SUB_ACTIVITY && resultCode == RESULT_OK) {
			String result = data.getStringExtra(SUB_RESULT);
			TextView resultView = (TextView) findViewById(R.id.resultview);
			resultView.setText("Result: " + result);
		} else {
			Log.e("Main", "Something wrong here!");
		}
	}

}
